package com.lcs.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lcs.springcloud.feign.StockFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController
{
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StockFeign stockFeign;

    @Value(("${server.port}"))
    private String port;

    @GetMapping("/add/{num}")
    @SentinelResource(value="/add/num", blockHandler = "block")
    public String add(@PathVariable int num)
    {
        System.out.println("下单成功");
//        String res = stockFeign.reduct();
//        String res = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        System.out.println("下单成功:"+num);
        return port+"下单成功"+num;
    }
    public String block( int num,BlockException e)
    {
        return "热点限流";
    }
}
